<?php 

namespace app\common\shop;

use think\Db;
use think\Model;
use traits\model\SoftDelete;


class CartModel extends model {
    //use SoftDelete;//开启了软删除
    //protected  = 'del_time';
    // 完整数据表名
    protected $table = 'shop_cart';


    /**
     * [建表]
     * @return
     */
    public static function createTable(){
        return Db::query("CREATE TABLE If Not Exists `shop_cart` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
             `uid` int(11) NOT NULL COMMENT '会员id',
             `goodsid` int(11) NOT NULL DEFAULT '0' COMMENT '商品id',
             `speclistid` int(11) NOT NULL DEFAULT '0' COMMENT '规格id',
             `speclistname` varchar(255)  DEFAULT NULL COMMENT '规格名称',
             `createtime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
             `number` int(11) NOT NULL DEFAULT '0' COMMENT '数量',
             `updatetime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
             PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='购物车'");
    }

    /**
     * [添加]
     * @return
     */
    public static function add($data){
        $data['createtime'] = time();
        $data['updatetime'] = time();
        return self::insertGetId($data);
    }

    /**
     * [修改]
     * @return
     */
    public static function edit($data,$id){
        $data['updatetime'] = time();
        return self::where('id',$id)->update($data);
    }

    /**
     * [获取审核状态]
     * @param
     * @return
     */
    public static function getCartIdInfo($cartid){
        return self::where('id',$cartid)->find()->toArray();
    }
    /**
     * [购物车商品]
     * @param
     * @return
     */
    public static function getCartGoods($cart){
        $goods = ShopGoodsModel::getGoods($cart['goodsid'],'id,title,price,old_prices,team_prices,status,path,total,shoptype')->toArray();
        $goods['img'] = unserialize($goods['path'])[0];
        if($cart['speclistid']){
            $speclist_data = SpecListModel::getInfo($cart['speclistid']);// 规格信息
            $goods['price'] = $speclist_data['price'];//价格
            $goods['team_prices'] = $speclist_data['team_prices'];// 团购价
            $title = SpecModel::getSpecTitle($speclist_data['key'],$cart['goodsid']);
            $goods['spec_title'] = $title;
            $goods['spec_title_arr'] = explode(';',$title);// 规格数组（可用于前端自定义样式）
            $goods['total'] = $speclist_data['total'];//库存
            $goods['specid'] = $speclist_data['id'] ;// 规格id
        }
        return $goods;
    }
}
